package com.example.java8.optional;

import com.example.java8.lambda.Employee;

import java.util.Optional;

/**
 * TODO
 *
 * @author qibao
 * @version v0.1 2020/12/17
 */
public class OptionalDemo {

    public static void main(String[] args) {
        Optional empty = Optional.empty();
        String s = "ss";
        Optional<String> of = Optional.of(s);
        Optional nullable = Optional.ofNullable(null);//s 可为空
        System.out.println(empty.orElse("OK"));
        System.out.println(of.isPresent());
        of.orElse("OK");
        of.orElseGet(String::new);
        of.ifPresent(System.out::println);
        Optional<Object> length = of.flatMap((x) -> Optional.of(x.length()));


        String o = new String();
        Optional optional = Optional.of(o);
        Optional.ofNullable(o);
        Optional.empty();
        optional.orElse(o);
        optional.isPresent();
        optional.orElseGet(() -> o);
        optional.map((x) -> o);
        Optional<Object> t = optional.flatMap((x) -> o);

    }

    private static String getGoleName(Optional<Man> man) {
        return man.orElse(new Man())
                .getGoldness().orElse(new Goldness("OK"))
                .getName();
    }
}
